package com.wdd.recoveryvehicle.employeeUser.controller;

import com.wdd.common.Result.Result;
import com.wdd.common.Result.ResultUtil;
import com.wdd.common.json.JsonUtil;
import com.wdd.common.utils.SnowFlake;
import com.wdd.recoveryvehicle.employeeUser.bean.EmpInfoVB;
import com.wdd.recoveryvehicle.employeeUser.bean.EmpTypeVB;
import com.wdd.recoveryvehicle.employeeUser.service.EmployeeUserService;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.hibernate.validator.constraints.NotBlank;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 人员信息
 *
 * @author globalMr
 */
@Validated
@RequestMapping("/empUser")
@RestController
public class EmployeeUserController {
    private Logger logger = LoggerFactory.getLogger(EmployeeUserController.class);

    private SnowFlake snowFlake = new SnowFlake(1, 1);

    @Autowired
    private EmployeeUserService employeeUserService;

    /**
     * 插入 人员信息
     *
     * @param empInfoVB
     * @return
     */
    @PostMapping("/insertEmpInfo")
    public Result insertEmpInfo(@Param("empInfoVB") String empInfoVB,
                                @NotBlank(message = "user_id为空") @Param("user_id") String user_id,
                                @NotBlank(message = "company_id为空") @Param("company_id") String company_id) {
        logger.info("insertEmpInfo === 参数 " + empInfoVB);

        EmpInfoVB empInfo = JsonUtil.toObject(empInfoVB, EmpInfoVB.class);

        // 生成人员ID
        String id = "emp" + snowFlake.nextId() + "";
        empInfo.setEmp_id(id);
        boolean flag = employeeUserService.insertEmpInfo(empInfo, user_id, company_id);

        if (flag) {
            return ResultUtil.success();
        } else {
            return ResultUtil.error(ResultUtil.ERROR_CODE, "插入人员信息失败");
        }
    }

    /**
     * 查询 人员信息
     *
     * @param useful
     * @return
     */
    @PostMapping("/selectEmpInfoVBByUseful")
    @RequiresRoles(value = {"vip", "svip"}, logical = Logical.OR)
    public Result selectEmpInfoVBByUseful(@NotBlank(message = "useful为空") String useful,
                                          @NotBlank(message = "company_id为空") @Param("company_id") String company_id) {
        logger.info("selectEmpInfoVBByUseful === 参数 " + useful);
        List<EmpInfoVB> empInfoVBS = employeeUserService.selectEmpInfoVBByUseful(useful, company_id);
        return ResultUtil.success(empInfoVBS);
    }

    /**
     * 查询 人员信息 --- 人员id
     *
     * @param vehicle_id
     * @return
     */
    @PostMapping("/selectEmpInfoVBByVehicleId")
    @RequiresRoles(value = {"vip", "svip"}, logical = Logical.OR)
    public Result selectEmpInfoVBByVehicleId(@NotBlank(message = "vehicle_id为空") String vehicle_id) {
        logger.info("selectEmpInfoVBByVehicleId === 参数 " + vehicle_id);
        List<EmpInfoVB> empInfoVBS = employeeUserService.selectEmpInfoVBByVehicleId(vehicle_id);
        return ResultUtil.success(empInfoVBS);
    }

    /**
     * 查询 公司的人员数据 --- 公司id
     *
     * @param create_company
     * @return
     */
    @PostMapping("/selectEmpIdNamePhone")
    public Result selectEmpIdNamePhone(@NotBlank(message = "create_company为空") String create_company) {
        logger.info("selectEmpIdNamePhone === 参数 " + create_company);
        List<EmpInfoVB> empInfoVBS = employeeUserService.selectEmpIdNamePhone(create_company);
        return ResultUtil.success(empInfoVBS);
    }

    /**
     * 查询 客户下面是否绑定车辆信息
     *
     * @param emp_id
     * @return
     */
    @PostMapping("/selectCountEmpVehicle")
    public Result selectCountEmpVehicle(@NotBlank(message = "emp_id为空") String emp_id) {
        logger.info("selectCountEmpVehicle === 参数 " + emp_id);
        List<String> vehicleNames = employeeUserService.selectCountEmpVehicle(emp_id);
        return ResultUtil.success(vehicleNames);
    }

    /**
     * 查询 人员类型
     *
     * @return
     */
    @PostMapping("/selectEmpTypeVB")
    public Result selectEmpTypeVB() {
        List<EmpTypeVB> empTypeVBS = employeeUserService.selectEmpTypeVB();
        return ResultUtil.success(empTypeVBS);
    }

    /**
     * 更新 数据信息
     *
     * @param empInfoVB
     * @return
     */
    @RequiresPermissions({"update"})
    @PostMapping("/updateEmpInfoVB")
    public Result updateEmpInfoVB(@Param("empInfoVB") String empInfoVB,
                                  @NotBlank(message = "user_id为空") @Param("user_id") String user_id) {
        logger.info("updateEmpInfoVB === 参数 " + empInfoVB);

        EmpInfoVB empInfo = JsonUtil.toObject(empInfoVB, EmpInfoVB.class);

        boolean flag = employeeUserService.updateEmpInfoVB(empInfo, user_id);
        if (flag) {
            return ResultUtil.success();
        } else {
            return ResultUtil.error(ResultUtil.ERROR_CODE, "更新人员信息失败");
        }
    }

    /**
     * 更新 人员信息无效
     *
     * @param emp_id
     * @return
     */
    @RequiresPermissions({"delete"})
    @PostMapping("/updateEmpInfoUsefulWx")
    public Result updateEmpInfoUsefulWx(@NotBlank(message = "emp_id为空") String emp_id,
                                        @NotBlank(message = "user_id为空") String user_id) {
        logger.info("updateEmpInfoUsefulWx === 参数 " + emp_id);

        boolean flag = employeeUserService.updateEmpInfoUsefulWx(emp_id, user_id);
        if (flag) {
            return ResultUtil.success();
        } else {
            return ResultUtil.error(ResultUtil.ERROR_CODE, "更新人员信息失败");
        }
    }


}
